<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Introduction to Jython Swing</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="GUI, Swing, Jython, programming, Linux, Introduction">
<meta name="description" content="Introduction to Jython Swing">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Introduction to Jython Swing</h1>

<p>
In this part of the Jython Swing tutorial, we will introduce the Swing toolkit and
create our first programs using the Jython programming language.
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 

<p>
The purpose of this tutorial is to get you started with the Swing toolkit with 
the Jython language. Images used in this
tutorial can be downloaded <a href="/img/gui/jrubyswing/images.zip">here</a>. 
I used some icons from the Tango icons pack of the Gnome project. 
</p>


<h2>About</h2>

<p>
<b>Swing</b> library is an official Java GUI toolkit for the Java
programming language. It is used to create Graphical user interfaces with Java. 
Swing is an advanced GUI toolkit. It has a rich set of components. From 
basic ones like buttons, labels, scrollbars to advanced components 
like trees and tables. Swing itself is written in Java. Swing is available
for other languages too. For example Jython, JRuby, Groovy or Scala.
</p>

<p>
<b>Jython</b> is an implementation of the Python programming language written in Java.
Jython can import any Java class. 
</p>

<p>
There are two basic ways to execute the examples in this tutorial. One way
is to install a Python NetBeans plugin. It contains Jython as well. 
When you create a new Python project, be sure to select the Jython platform.
</p>

<p>
The other way is to download an installer from the <a href="http://jython.org">jython.org</a>
website.
</p>

<pre>
$ java -jar jython_installer-2.5.2rc2.jar 
</pre>

<p>
We install the Jython. You go through a series of dialogs. 
</p>

<pre>
$ java -jar jython.jar simple.py
</pre>

<p>
We have installed Jython in a selected directory. In this directory, we will find
jython.jar file, which is used to execute Jython scripts. 
</p>

<pre>
$ cat /usr/local/bin/jython 
#!/bin/bash

/home/vronskij/bin/jdk1.6.0_21/bin/java -jar /home/vronskij/bin/jython/jython.jar $1
</pre>

<p>
Optionally, we can create a bash file which will automatically start our Jython 
scripts. We can then put the #!/usr/bin/local/jython path to our scripts. 
</p>


<h2>Simple example</h2>

<p>
In our first example, we will show a basic window on the screen. 
</p>

<pre class="code">
#!/usr/local/bin/jython
# -*- coding: utf-8 -*-

"""
ZetCode Jython Swing tutorial

This example shows a simple 
window on the screen.

author: Jan Bodnar
website: www.zetcode.com
last modified: November 2010
"""

from javax.swing import JFrame


class Example(JFrame):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        self.setTitle("Simple")
        self.setSize(250, 200)
        self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
        self.setLocationRelativeTo(None)
        self.setVisible(True)


if __name__ == '__main__':
    Example()
</pre>

<p>
While this code is very small, the application window can do quite a lot. 
It can be resized, maximized, minimized. All the complexity that comes 
with it has been hidden from the application programmer. 
</p>


<pre class="explanation">
from javax.swing import JFrame
</pre>

<p>
We import a <code>JFrame</code> class. The <code>JFrame</code> is a top-level 
window with a title and a border.
</p>

<pre class="explanation">
self.initUI()
</pre>

<p>
We delegate the creation of the user interface to the <code>initUI()</code> method.
</p>

<pre class="explanation">
self.setTitle("Simple")
</pre>

<p>
We set the title of the window using the <code>setTitle()</code> method.
</p>

<pre class="explanation">
self.setSize(250, 200)
</pre>

<p>
We set the size of the window. 
</p>

<pre class="explanation">
self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
</pre>

<p>
This method ensures that the window terminates, if we click on the
close button of the titlebar. By default nothing happens. 
</p>

<pre class="explanation">
self.setLocationRelativeTo(None)
</pre>

<p>
We center the window on the screen. 
</p>

<pre class="explanation">
self.setVisible(True)
</pre>

<p>
Finally, the window is showed on the screen. 
</p>


<h2>Tooltip</h2>

<p>
A tooltip is a small rectangular window, which
gives a brief information about an object. 
It is usually a GUI component. It is part of
the help system of the application. 
</p>

<pre class="code">
#!/usr/local/bin/jython
# -*- coding: utf-8 -*-

"""
ZetCode Jython Swing tutorial

This code shows a tooltip on
a window and a button.

author: Jan Bodnar
website: www.zetcode.com
last modified: November 2010
"""

from javax.swing import JButton
from javax.swing import JFrame
from javax.swing import JPanel

class Example(JFrame):
    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):
        
        panel = JPanel()
        self.getContentPane().add(panel)

        panel.setLayout(None)
        panel.setToolTipText("A Panel container")

        button = JButton("Button")
        button.setBounds(100, 60, 100, 30)
        button.setToolTipText("A button component")

        panel.add(button)

        self.setTitle("Tooltips")
        self.setSize(300, 200)
        self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
        self.setLocationRelativeTo(None)
        self.setVisible(True)


if __name__ == '__main__':
    Example()
</pre>

<p>
In the example, we set the tooltip for the frame and the button.
</p>

<pre class="explanation">
panel = JPanel()
self.getContentPane().add(panel)
</pre>

<p>
We create a <code>JPanel</code> component. It is a generic lightweight 
container. <code>JFrame</code> has an area, where you put the components 
called the <b>content pane</b>. We put the panel into this pane.
</p>

<pre class="explanation">
panel.setLayout(None)
</pre>

<p>
By default, the <code>JPanel</code> has a <code>FlowLayout</code> manager. The layout
manager is used to place widgets onto the 
containers. If we call <code>setLayout(None)</code> we can position our 
components absolutely. For this, we use the 
<code>setBounds()</code> method.
</p>

<pre class="explanation">
panel.setToolTipText("A Panel container")
</pre>

<p>
To enable a tooltip, we call the <code>setTooltipText()</code> method.
</p>

<br>
<img src="/img/gui/jrubyswing/tooltip.png" alt="Tooltip">
<div class="figure">Figure: Tooltip</div>


<h2>Quit button</h2>

<p>
In the last example of this section, we will create a quit button. 
When we press this button, the application terminates. 
</p>

<pre class="code">
#!/usr/local/bin/jython
# -*- coding: utf-8 -*-

"""
ZetCode Jython Swing tutorial

This program creates a quit
button. When we press the button,
the application terminates.

author: Jan Bodnar
website: www.zetcode.com
last modified: November 2010
"""

from java.lang import System
from javax.swing import JButton
from javax.swing import JFrame
from javax.swing import JPanel


class Example(JFrame):
    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        panel = JPanel()
        self.getContentPane().add(panel)

        panel.setLayout(None)

        qbutton = JButton("Quit", actionPerformed=self.onQuit)
        qbutton.setBounds(50, 60, 80, 30)

        panel.add(qbutton)

        self.setTitle("Quit button")
        self.setSize(300, 200)
        self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
        self.setLocationRelativeTo(None)
        self.setVisible(True)

    def onQuit(self, e):
        System.exit(0)


if __name__ == '__main__':
    Example()
</pre>

<p>
We position a <code>JButton</code> on the window. We will add an action listener
to this button.
</p>


<pre class="explanation">
qbutton = JButton("Quit", actionPerformed=self.onQuit)
qbutton.setBounds(50, 60, 80, 30)
</pre>

<p>
Here we create a button. We position it by calling the <code>setBounds()</code> 
method. The <code>actionPerformed</code> parameter specifies the method which is
called, when we click on the button.
</p>

<pre class="explanation">
def onQuit(self, e):
    System.exit(0)
</pre>

<p>
The onQuit() method exits the application.
</p>

<br>
<img src="/img/gui/jrubyswing/quitbutton.png" alt="Quit button">
<div class="figure">Figure: Quit button</div>

<hr class="btm">

<p>
This section was an introduction to the Swing toolkit with the Jython language.
</p>

<br>
<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified December 13, 2010  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

